package com.ruoyi.littleRedBook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.littleRedBook.domain.pojo.Notification;
import com.ruoyi.littleRedBook.mapper.NotificationMapper;
import com.ruoyi.littleRedBook.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements NotificationService {

    @Autowired
    private NotificationMapper notificationMapper;

    private Long getCurrentUserId() {
        return 1L; // 替换为真实当前用户ID获取逻辑
    }

    @Override
    public AjaxResult getNotificationList(Integer pageNum, Integer pageSize, Integer type) {
        Long userId = getCurrentUserId();
        Page<Notification> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);

        // 按类型筛选（0为全部）
        if (type != 0) {
            queryWrapper.eq("type", type);
        }

        queryWrapper.orderByDesc("created_at"); // 按创建时间倒序（最新在前）
        Page<Notification> notificationPage = notificationMapper.selectPage(page, queryWrapper);
        return AjaxResult.success(notificationPage.getRecords());
    }

    @Override
    public AjaxResult markAsRead(Long id) {
        Long userId = getCurrentUserId();
        // 校验通知归属
        Notification notification = notificationMapper.selectById(id);
        if (notification == null || !notification.getUserId().equals(userId)) {
            return AjaxResult.error("通知不存在或无权限");
        }

        // 标记为已读
        notification.setIsRead(1);
        int rows = notificationMapper.updateById(notification);
        return rows > 0 ? AjaxResult.success("标记成功") : AjaxResult.error("标记失败");
    }

    @Override
    public AjaxResult markAllAsRead() {
        Long userId = getCurrentUserId();
        int rows = notificationMapper.updateAllReadByUserId(userId);
        return AjaxResult.success("成功标记" + rows + "条通知为已读");
    }

    @Override
    public AjaxResult deleteNotification(Long id) {
        Long userId = getCurrentUserId();
        // 校验通知归属
        Notification notification = notificationMapper.selectById(id);
        if (notification == null || !notification.getUserId().equals(userId)) {
            return AjaxResult.error("通知不存在或无权限");
        }

        int rows = notificationMapper.deleteById(id);
        return rows > 0 ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @Override
    public AjaxResult getUnreadCount() {
        Long userId = getCurrentUserId();
        QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                    .eq("is_read", 0);
        Long count = notificationMapper.selectCount(queryWrapper);
        return AjaxResult.success(count);
    }
}
